package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = Exception.class)
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;
    /**
     * 根据医院查询
     *
     * @param hoscode
     **/
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hopital = hospitalRepository.getHopitalByHoscode(hoscode);
        return hopital;
    }



    /**
     * 上传医院接口
     *
     * @param paramMap
     **/
    @Override
    public void save(Map<String, Object> paramMap) {
        //为了方便操作，将参数map集合转换成对象Hospital
        //利用fastjson中的方法，map集合先装换成字符
        String mapString = JSONObject.toJSONString(paramMap);
        //将字符类型转换成Hospital对象
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHopitalByHoscode(hoscode);
        //如果存在，进行修改
        if(hospitalExist != null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else{
            //如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    /**
     * 医院列表
     *
     * @param page
     * @param limit
     * @param hospitalSetQueryVo
     **/
    @Override
    public Page selectHospPage(Integer page, Integer limit, HospitalSetQueryVo hospitalSetQueryVo) {
        /**创建pageable对象**/
        Pageable pageable = PageRequest.of(page-1,limit );
        /**创建条件**/
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        /**hospitalSetQueryVo换成Hospital**/
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalSetQueryVo,hospital);
        /**创建对象**/
        Example<Hospital> example = Example.of(hospital,matcher);
        /**调用方法实现调用**/

        Page<Hospital> pages = hospitalRepository.findAll(example,pageable);

        List<Hospital> content = pages.getContent();
        /**获取查询list集合，遍历医院等级**/
        pages.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });
        return pages;

    }
        /**获取查询list集合，遍历医院等级封装**/
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());

        //查询省 市 地区
        String provinceString = dictFeignClient.getName( hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName( hospital.getDistrictCode());
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;

    }

    /**
     * 更新医院上线
     *
     * @param id
     * @param status
     **/
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 通过id获得医院详细信息
     *
     * @param id
     **/
    @Override
    public Map<String, Object> getHospById(String id) {
       Map<String, Object> result = new HashMap<>();
       Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
       //医院基本信息
        result.put("hospital",hospital);
        //单独处理
        result.put("bookingRule",hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    /**
     * 根据医院名称获取
     *
     * @param hoscode
     **/
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital != null){
            return hospital.getHosname();
        }
        return null;
    }

    /**
     * 根据医院名称查询
     *
     * @param hosname
     **/
    @Override
    public List<Hospital> findByHosname(String hosname) {
        return  hospitalRepository.getHospitalByHosname(hosname);

    }

    /**
     * 根据医院编号获取医院预约挂号详情
     *
     * @param hoscode
     **/
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        //医院基本信息
        result.put("hospital",hospital);
        //单独处理
        result.put("bookingRule",hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }
}
